package org.southyin.exception;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Date;

@Component
@Aspect
public class AopConfig {
    @Pointcut("execution(* org.southyin.exception.*.*(..))")
    public void cutOne(){}

    @AfterThrowing(value = "org.southyin.exception.AopConfig.cutOne()", throwing = "e")
    public void throwing(Exception e) {
        File file = new File("e:/exceptionsLog.txt");
        FileWriter writer = null;
        PrintWriter out = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            writer = new FileWriter(file, true);
            out = new PrintWriter(writer,true);
            out.println(new Date().toLocaleString());
            out.println("出现以下异常信息：");
            out.println(e.getStackTrace());
            out.println("------------------------------");
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            out.close();
            try {
                writer.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
